﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// add
using System.Windows.Forms;  // application
using System.IO; // Path
using System.Reflection; // Assemply

namespace FormBaseLib
{
    public class CApplication: CBase
    {
        //bool mbMyDebug;

        public string msCompanyID { get; set; }
        public string msSystemID { get; set; }
        public string msProjectID { get; set; }
        public DateTime mTimeStart { get; set; }
        public DateTime mTimeEnd { get; set; }
        public DateTime mTimeRelease { get; set; }
        public string msProductName { get; set; }
        public string msProductVersion { get; set; }
        public string msNamespace { get; set; }

        public string msPathExeRoot { get; set; }
        public string msPathDataUserRoaming { get; set; }
        public string msPathDataUserAll { get; set; }
        public string msPathDataUserLocal { get; set; }


        public string msPathRoot { get; set; }
        public string msPathData { get; set; }
        public string msPathLog { get; set; }
        public string msPathSystemTemp { get; private set; } //system's temporary folder
        public string msPathLastUsed { get; set; }

        public string msAssemblyVersion { get; set; }
        public enumEnvironment mEnvironment;

        public long mlCounter { get; private set; }
        public DateTime mTimeResetCounter { get; private set; }

        public bool mbDebug
        {
            get { return mAction.mbDebug; }
            set { mAction.mbDebug = value; }
        }

        public string[] msaArgs { get; set; }

        public CAction mAction = new CAction();
        //public CLastError mLastError = new CLastError();


        public CApplication()
        {
            string s1;

            mEnvironment = enumEnvironment.Production;
            mbDebug = false;

            msCompanyID = "CompanyID";
            msSystemID = "SystemID";
            msProjectID = "ProjectID";
            msNamespace = @"http://www.luckstar.com.tw";   //http://tempuri.org/DataSetProjectRun.xsd

            mTimeStart = DateTime.Now;
            mTimeEnd = mTimeStart;
            mTimeRelease = new DateTime(2014, 6, 26, 11, 2, 0);
            msProductName = Application.ProductName;
            msProductVersion = Application.ProductVersion;

            // Windows predefines:
            // i.e.
            //msPathAppStartup = "D:\\...\\FormBase10\\FormBase\\FormBase\\bin\\Debug"
            //msPathAppDataUserRoaming = "C:\\Users\\honda_chen\\AppData\\Roaming"
            //msPathAppDataUserAll = "C:\\ProgramData"
            //msPathAppDataUserLocal = "C:\\Users\\honda_chen\\AppData\\Local"
            //msPathAppTemp = "C:\\Users\\honda_chen\\AppData\\Local\\Temp\\"
            msPathDataUserRoaming = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);  // current roaming user
            msPathDataUserAll = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);  // all user
            msPathDataUserLocal = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);   // local user only
            msPathExeRoot = Application.StartupPath; // The .exe Startup Path.
            s1 = Path.GetTempPath();
            msPathSystemTemp = s1.Remove(s1.Length - 1, 1);  // Consistency with aboves: no "Back-Slash" in suffix.

            msAssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            ResetCounter();
            mAction.BeginAction("CApplication.Begin()");

        }
        public void PopupMsg(string sMsg)
        {
            MessageBox.Show(sMsg, msProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public void PopupError(string sMsg)
        {
            MessageBox.Show(sMsg, msProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        public bool AskMsg(string sMsg)
        {
            return (MessageBox.Show(sMsg, msProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes);
        }

        public string GetVersionDisplay()
        {
            switch (mEnvironment)
            {
                case enumEnvironment.Development:
                    return "| V " + msAssemblyVersion + " D";

                case enumEnvironment.Testing:
                    return "| V " + msAssemblyVersion + " T";


                default:
                    return "| V " + msAssemblyVersion;
            }

        }

        public void SetCurrentCounter(long lLastCounter, DateTime tLastReset)
        {
            mlCounter = lLastCounter;
            mTimeResetCounter = tLastReset;
        }
        public void ResetCounter()
        {
            mlCounter = -1;
            mTimeResetCounter = DateTime.Now;
        }
        public long GetNextCounter()
        {
            if (mlCounter >= 9999999999)
                ResetCounter();

            mlCounter++;
            return mlCounter;
        }
        public void ShowFormActionLog(CAction pCAction)
        {
            //if (CProject.mApp.mbDebug)
            //{
            //FActionLog f1 = new FActionLog(mApp.mAction.msMessageLog, string.Format("{0}.txt", DateTime.Now.ToString("yyyyMMdd-HHmmss")), "Action Log");
            FActionLog f1 = new FActionLog();

            f1.SetActionLog(pCAction);

            f1.Show();
            f1.Focus();
            //}
        }

    }
}
